package formulation.taglibs;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import db.ICommonObject;

public class ListTag extends TagSupport
{
    private static final long serialVersionUID = 1L;
    
    private String id;
    private String defaultId = null;
    private List<ICommonObject> list;
    
    public void setList(List<ICommonObject> list) {
        this.list = list;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public void setDefaultId(String id){
       this.defaultId = id;
    }
    @Override
    public int doStartTag() throws JspException
    {
        JspWriter out = pageContext.getOut();
        try
        {
            out.print("<select id='" + id + "'>");
            for (int i = 0; i < list.size(); i++) {
            	
            	if(defaultId==null)
            	{out.print("<option value=\"" + list.get(i).getId() + "\">");}
            	else if(defaultId.equals( list.get(i).getId()+""))
            	{out.print("<option value='" + list.get(i).getId() + "' selected>");}
            	else
            	{ out.print("<option value=\"" + list.get(i).getId() + "\">");}
                out.print(list.get(i).getName());
                out.print("</option>");
            }
            out.print("</select>");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
    
    

}
